package com.wave.wavesystem.serve.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.wave.common.util.Func;
import com.wave.wavesystem.ai.enums.MessageRole;
import com.wave.wavesystem.serve.enums.post.PostStatusEnum;
import com.wave.wavesystem.serve.mapper.PostMapper;
import com.wave.wavesystem.serve.pojo.entity.Post;
import com.wave.wavesystem.serve.pojo.query.CenterSearchQuery;
import com.wave.wavesystem.serve.pojo.query.PostQuery;
import com.wave.wavesystem.serve.service.IPostService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IPostService {

    @Override
    public QueryChain<Post> getQueryChain(PostQuery query) {
        QueryChain<Post> queryChain = queryChain();
        if (Func.isNotEmpty(query.getId())) {
            queryChain.eq(Post::getId, query.getId());
        }
        if (Func.isNotEmpty(query.getStatus()) && query.getStatus() == PostStatusEnum.CHECK.getCode()) {
            queryChain.eq(Post::getStatus, PostStatusEnum.CHECK.getCode());
        }
        queryChain.orderBy(Post::getPublishTime, false);
        return queryChain;
    }

    @Override
    public QueryChain<Post> getQueryChain(PostQuery query, boolean isPublish) {
        QueryChain<Post> queryChain = getQueryChain(query);
        if (isPublish) {
            queryChain.eq(Post::getStatus, 2)
                    .isNotNull(Post::getPublishTime);
        }
        return queryChain;
    }

    @Override
    public boolean approvalById(Long postId, String role) {
        MessageRole byRole = MessageRole.findByRole(role);
        UpdateChain<Post> updateChain = this.updateChain().eq(Post::getId, postId)
                .set(Post::getStatus, PostStatusEnum.PUBLISH.getCode())
                .set(Post::getPublishTime, Func.now_YYYY_MM_DD_MM_HH_SS());
        if (byRole == MessageRole.ASSISTANT) {
            updateChain.set(Post::getRemark, "本次审批由ai通过");
        }
        return updateChain.update();
    }

    @Override
    public boolean rejectPost(Long postId, String rejectReason) {
        UpdateChain<Post> updateChain = this.updateChain().eq(Post::getId, postId)
                .set(Post::getStatus, PostStatusEnum.REJECT.getCode())
                .set(Post::getRemark, "本次审批由ai拒绝，原因是：" + rejectReason);
        return updateChain.update();
    }

    @Override
    public Page<Post> search(CenterSearchQuery searchQuery) {
        String keyword = searchQuery.getKeyword();
        QueryChain<Post> queryChain = this.queryChain()
                .like(Post::getTitle, keyword)
                .or(Post::getContent)
                .like(keyword)
                .or(Post::getDesc)
                .like(keyword);
        return this.page(new Page<>(searchQuery.getPageNum(), searchQuery.getPageSize()), queryChain);
    }

}
